package com.freeunion.zhibo.views.customviews;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.WindowManager;

import com.freeunion.zhibo.model.MySelfInfo;
import com.freeunion.zhibo.utils.Constants;
import com.freeunion.zhibo.utils.LogConstants;
import com.freeunion.zhibo.utils.SxbLog;
import com.freeunion.zhibo.utils.UiUtils;

/**
 * Created by admin on 2016/5/20.
 */
public class BaseActivity extends Activity {
    private AlertDialog loadingDialog;

    private BroadcastReceiver recv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        recv = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Constants.BD_EXIT_APP)){
                    SxbLog.d("BaseActivity", LogConstants.ACTION_HOST_KICK + LogConstants.DIV + MySelfInfo.getInstance().getId() + LogConstants.DIV + "on force off line");
                    finish();
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.BD_EXIT_APP);

        registerReceiver(recv, filter);
    }

    @Override
    protected void onDestroy() {
        try {
            unregisterReceiver(recv);
        }catch (Exception e){
        }
        super.onDestroy();
    }

    protected void showLoading() {
        if (loadingDialog == null)
            loadingDialog = UiUtils.newLoadingDialog(this);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
            WindowManager.LayoutParams layoutParams = loadingDialog.getWindow().getAttributes();
            layoutParams.width = UiUtils.dip2px(this,150);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            layoutParams.dimAmount = 0.0f;
            loadingDialog.getWindow().setAttributes(layoutParams);
        }
    }

    protected void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }
}
